Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Ocsigen Server without configuration file (and with static linking) #238

Merged
merged 32 commits into from
Aug 28, 2024

Conversation

balat
Copy link
Member

@balat balat commented Apr 27, 2024

Almost ready.

Todo:

  • Clean
  • Test with Ocsigen Start

And in other projects/branches/PRs:

  • Doc
  • A few missing extensions (extendconfiguration, ...?)
  • Update distillery templates

Example: To add a Web server to your OCaml program, serving static files from directory "static":

let _ = Ocsigen_server.start [ Ocsigen_server.host [Staticmod.run ~dir:"static" ()]]

Install:

opam pin add -n ocsigenserver [email protected]:ocsigen/ocsigenserver.git#config
opam install ocsigenserver

Example of Dune file:

(executable
 (public_name Ololo)
 (name main)
 (libraries
  main
  ocsigenserver
  ocsigenserver.ext.staticmod))

Compile with:

dune build

A more complex example without Eliom:

let _ = Ocsigen_server.start [ Ocsigen_server.host ~regexp:".*"
        [ Accesscontrol.(
            if_
              (and_
                 [ ip "127.0.0.1"
                 ; header ~name:"user-agent" ~regexp:".*Safari.*"
                 ; method_ `POST ])
              [forbidden] [])
        ; Redirectmod.run
            ~redirection:
              (Redirectmod.create_redirection ~full_url:false ~regexp:"^p.*$"
                 "toto.html")
            ()
        ; Authbasic.run ~realm:"pouette"
            ~auth:(fun _u p -> Lwt.return (p = "toto"))
            ()
        ; Staticmod.run ~dir:"statico" ()
        ; Staticmod.run ~dir:"static" ()
        ; Cors.run ~credentials:false ()
        ; Deflatemod.run ~mode:(`All_but []) () ] ]
]]

Here is an example of a complex configuration using Server and Eliom without config file with the new interface:

let _ = Ocsigen_config.set_debug ()

let f s _ () =
  Lwt.return
    Eliom_content.Html.F.(html (head (title (txt "")) []) (body [h1 [txt s]]))

(* My first Eliom app has default name *)
let myservice =
  Eliom_service.create ~path:(Eliom_service.Path ["aa"])
    ~meth:(Eliom_service.Get Eliom_parameter.any) ()

let () = Eliom_registration.Html.register ~service:myservice (f "Default")

(* My second Eliom app is called "e" *)
let _ = Eliom.set_app_name "e"

let myservice =
  Eliom_service.create ~path:(Eliom_service.Path ["aa"])
    ~meth:(Eliom_service.Get Eliom_parameter.any) ()

let () = Eliom_registration.Html.register ~service:myservice (f "e")

(* Configuring Ocsigen Server:  *)
let _ =
  Ocsigen_server.start ~debugmode:true ~veryverbose:()
    [ Ocsigen_server.host ~re:"a" [Staticmod.run ~dir:"static" ()]
    ; Ocsigen_server.host ~re:".*"
        [ Redirectmod.run
            ~redirection:
              (Redirectmod.create_redirection ~full_url:`No ~regexp:"^p.*$"
                 "toto.html")
            ()
        ; Authbasic.run ~realm:"pouette"
            ~auth:(fun _u p -> Lwt.return (p = "b"))
            ()
        ; Staticmod.run ~dir:"statico" ()
        ; Staticmod.run ~dir:"static" ()
        ; (* Default Eliom app: *)
          Eliom.run ()
        ; (* Second Eliom app on site "ee" *) 
          Ocsigen_server.site ["ee"] [Eliom.run ~app:"e" ()]
        ; (* others *)
          Cors.run ~credentials:false ()
        ; Deflatemod.run ~mode:(`All_but []) () ] ]

To compile, create this dune file:

(executable
 (public_name stata)
 (name main)
 (libraries
  stata
  ocsigenserver
  ocsigenserver.ext.staticmod
  ocsigenserver.ext.authbasic
  ocsigenserver.ext.cors
  ocsigenserver.ext.deflatemod
  ocsigenserver.ext.redirectmod
  ocsipersist.dbm
  eliom.server))

and compile:

dune build

To test with Ocsigen Start:

opam pin add -n ocsigenserver [email protected]:ocsigen/ocsigenserver.git#config
opam pin add -n ocsipersist-lib [email protected]:ocsigen/ocsipersist.git#static
opam pin add -n ocsipersist [email protected]:ocsigen/ocsipersist.git#static
opam pin add -n ocsipersist-pgsql [email protected]:ocsigen/ocsipersist.git#static
opam pin add -n ocsipersist-pgsql-config [email protected]:ocsigen/ocsipersist.git#static
opam pin add -n eliom [email protected]:ocsigen/eliom.git#config
opam pin add -n ocsigen-toolkit [email protected]:ocsigen/ocsigen-toolkit.git#static
opam pin add -n ocsigen-start [email protected]:ocsigen/ocsigen-start.git#static
opam install ocsigen-start
git clone [email protected]:ocsigen/os_template.git
cd os_template
git checkout static
make test.static.byte

Thanks to @vasilisp for the inspiration!

@balat balat changed the title Using Ocsigen Server without configiration file (and with static linking) Using Ocsigen Server without configuration file (and with static linking) May 2, 2024
@balat
Copy link
Member Author

balat commented May 20, 2024

This should be ready to test.
@vouillon @hhugo @Wonko7

@balat balat linked an issue May 21, 2024 that may be closed by this pull request
@balat balat marked this pull request as ready for review May 22, 2024 20:16
@balat balat force-pushed the config branch 3 times, most recently from f50565b to f0a3175 Compare June 7, 2024 20:24
@balat balat marked this pull request as draft June 28, 2024 09:53
This feature is not supported by cohttp.
In mirage/ocaml-cohttp#943,
Anil suggest to do that instead:
setcap 'cap_net_bind_service=+ep' <binary file>
to bind a low port to a non-priviledged user

I keep OCSIGENUSER in Makefile for install
will continue without the feature if creation fails.
It is created by the server anyway.
Was requested by users to package with Nix.
…ve parameters

Before: parameters were global to a site, which was making it impossible to have several times the same instruction in the same site
Removing Ocsigen_server.Site.register and compose instructions instead
@balat balat force-pushed the config branch 2 times, most recently from 86b0e4a to 932ab7a Compare August 21, 2024 14:30
@balat balat marked this pull request as ready for review August 22, 2024 15:36
@balat balat force-pushed the config branch 2 times, most recently from f9dbac5 to cf04286 Compare August 22, 2024 18:27
@balat balat force-pushed the config branch 5 times, most recently from 8d73f36 to 4f374fa Compare August 23, 2024 15:10
@balat balat merged commit 05a57ed into master Aug 28, 2024
15 of 19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Server without config file
1 participant